package com.eslserver.action.schedule;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eslserver.core.ESLServerPageDispatcher;
import com.eslserver.dao.DAOException;
import com.eslserver.dao.ESLServerDAOFactory;
import com.eslserver.dao.ScheduleDAO;

@SuppressWarnings("serial")
public class DeleteScheduleAction extends HttpServlet implements
		ESLServerPageDispatcher {
	public static final Logger LOG = LoggerFactory
			.getLogger(DeleteScheduleAction.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			ScheduleDAO scheduleDAO = ESLServerDAOFactory.getDAOFactory(
					ESLServerDAOFactory.ESLSERVER_DAOFACTORY).getScheduleDAO();

			String[] selectedCheckboxes = req
					.getParameterValues(PARAM_ALL_SELECTED_CB);

			if (selectedCheckboxes != null) {
				for (String scheduleID : selectedCheckboxes) {
					scheduleDAO.deleteSchedule(Integer.valueOf(scheduleID));
				}
			}

			req.setAttribute(ATTRIBUTE_TAB_ID, SCHEDULE_TAB_ID);
			RequestDispatcher dispatcher = req
					.getRequestDispatcher(MAIN_DASHBOARD);
			dispatcher.forward(req, resp);
		} catch (DAOException e) {
			LOG.error("error to delete Schedule", e);
		}
	}
}
